package com.sgb.kjwl.viewmodel.self;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.PersonInfoEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.personmanger.AddPersonActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddPersonModel {
    private AddPersonActivity activity;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public AddPersonModel(AddPersonActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    /*public void refresh() {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.delAddress("")
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity shoppingCarEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        activity.refresh(null);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        DialogHelper.dismissProgressDialog();
                        List<String> stringList = new ArrayList<>();
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        activity.refresh(stringList);
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                    }
                });

        //测试数据
    }*/

    /*public void lodaMore() {
        DialogHelper.showProgressDialog(activity.getActivity(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.delAddress(repertory.getAreaCode())
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity shoppingCarEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        activity.loadMore(null);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                        List<String> stringList = new ArrayList<>();
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        stringList.add("测试数据");
                        activity.loadMore(stringList);
                    }
                });

        //测试数据
    }*/
    public void personSearch(Map map) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getOrderAPI().personSearch(map).enqueue(new Callback<BaseEntity<PersonInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PersonInfoEntity>> call, Response<BaseEntity<PersonInfoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if ("ok".equals(response.body().getState())) {
                    activity.initDatas(response.body().getData());
                } else {
                    activity.empty();
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PersonInfoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void personAdd(Map map) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getOrderAPI().personAdd(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if ("ok".equals(response.body().getState())) {
                    MToast.showToast(activity,"关联成功");
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
}
